package com.ruoyi.framework.config;

import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import jakarta.servlet.Filter;
import jakarta.servlet.Servlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {
    private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class);

    @Value("${spring.datasource.dynamic.druid.monitor.allow:127.0.0.1}")
    private String allow;

    @Value("${spring.datasource.dynamic.druid.monitor.deny:}")
    private String deny;

    @Value("${spring.datasource.dynamic.druid.monitor.username:admin}")
    private String username;

    @Value("${spring.datasource.dynamic.druid.monitor.password:admin123}")
    private String password;

    @Value("${spring.datasource.dynamic.druid.monitor.resetEnable:false}")
    private String resetEnable;

    @Value("${spring.datasource.dynamic.druid.monitor.exclusions:*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*}")
    private String exclusions;

    /**
     * 配置Druid监控启动页面
     */
    @Bean
    @ConditionalOnMissingBean
    public ServletRegistrationBean<Servlet> druidStartViewServlet() {
        logger.info("Initializing Druid StatViewServlet with allow={}, username={}", allow, username);
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 白名单
        servletRegistrationBean.addInitParameter("allow", allow);
        // 黑名单
        if (!deny.isEmpty()) {
            servletRegistrationBean.addInitParameter("deny", deny);
        }
        // 登录查看信息的账密，用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", username);
        servletRegistrationBean.addInitParameter("loginPassword", password);
        // 是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable", resetEnable);
        return servletRegistrationBean;
    }

    /**
     * Druid监控过滤器配置规则
     */
    @Bean
    @ConditionalOnMissingBean
    public FilterRegistrationBean<Filter> filterRegistrationBean() {
        logger.info("Initializing Druid WebStatFilter with exclusions={}", exclusions);
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new WebStatFilter());
        // 添加过滤规则
        filterFilterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息
        filterFilterRegistrationBean.addInitParameter("exclusions", exclusions);
        // 可以添加其他参数，如session统计
        filterFilterRegistrationBean.addInitParameter("sessionStatEnable", "true");
        filterFilterRegistrationBean.addInitParameter("principalSessionName", "username");
        return filterFilterRegistrationBean;
    }
}